<?php

/**
 * Paciente actions.
 *
 * @package    salud_online
 * @subpackage Paciente
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class PacienteActions extends sfActionsCrud
{
  protected function complementList(sfWebRequest $request, kcDoctrineQuery $q)
  {
  	$parent_slug  = $request->getParameter('parent_slug');
  	$params = $request->getParameterHolder()->getAll();
  	$this->object = Doctrine::getTable('Usuario')->findOneBySlug($parent_slug);
  	Doctrine::getTable($this->dbClass)->getPacientesSecundarios($q, $this->object->getFamiliaId(), $params);
  }
  protected function complementSave(sfWebRequest  $request)
  {
    $this->redirect("@inicio");
  }
  protected function complementObj(sfWebRequest $request)
  {
  	if(!$this->obj)
  	{
  	  $this->usuario = Doctrine::getTable('Usuario')->findOneBySlug($request->getParameter('parent_slug'));
  	  $this->familia = $this->usuario->getFamilia(); 
      $this->forward404Unless($this->usuario);
  	  $this->obj    = new $this->dbClass();
  	  $this->obj->setFamilia($this->familia);
  	}
  }

  public function executeShow(sfWebRequest $request)
  {
  	$this->paciente = Doctrine::getTable('Paciente')->findOneBySlug($request->getParameter('slug'));
  }
  
  public function executeGraficos(sfWebRequest $request)
  {
    $informacion_peso = Doctrine::getTable('DetallePaciente')->getInformacionPeso(Doctrine::getTable('Paciente')->getPacientePrincipal($usuario_id)->getId());
	$chartData = array();

	for( $i = 0; $i < 7; $i++ )
	{
      $chartData[] = rand(1,20);
	}

	$bar = new stBarOutline( 80, '#78B9EC', '#3495FE' );
	$bar->key( 'Number of downloads per day', 10 );
	$bar->data = $chartData;

	$g = new stGraph();
	$g->title( 'stOfcPlugin example', '{font-size: 20px;}' );
	$g->bg_colour = '#E4F5FC';
	$g->set_inner_background( '#E3F0FD', '#CBD7E6', 90 );
	$g->x_axis_colour( '#8499A4', '#E4F5FC' );
	$g->y_axis_colour( '#8499A4', '#E4F5FC' );
	$g->data_sets[] = $bar;
	$g->set_x_labels(array( 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday' ));
	$g->set_x_label_style( 10, '#18A6FF', 0, 2 );
	$g->set_x_axis_steps( 2 );
	$g->set_y_max( max($chartData) );
	$g->y_label_steps( 4 );
	$g->set_y_legend( 'stOfcPlugin', 12, '#18A6FF' );
	echo $g->render();

	return sfView::NONE;
  }
  
  public function executeAutocomplete(sfWebRequest $request)
  { 
    $this->getResponse()->setContentType('application/json');
 
    $q = "%" . $request->getParameter('q') . "%";
    $limit = $request->getParameter('limit');
    
    $dql = kcDoctrineQuery::create()
           ->select('di.*, p.*, d.*')
           ->from('Distrito di')
           ->leftJoin('di.Provincia p')
           ->leftJoin('p.Departamento d')
           ->filterFieldByString('di.nombre', $q)
           ->limit($limit);
           
    $this->rows = $dql->fetchArray();
    //Deb::print_r($this->rows);
    $persons = array();
    foreach ($this->rows as $row)
    {
      $persons[$row['id']] = (string) $row['nombre'].' - '.$row['Provincia']['nombre'].' - '.$row['Provincia']['Departamento']['nombre'];
    }
  
    return $this->renderText(json_encode($persons));
  }
}
